<?php
/**
 * Master template class
 * This class only provides the prototype methods for child template classes.
 * For every dynamic web page, there are 2 parts that always change, they are
 * the head content (the content in the &lt;head&gt; tag) and the main dynamic
 * content. Therefore, the template classes have basic methods that render
 * these content. These classes also use a sole layout file for the static
 * content.
 * @package OWL
 * @subpackage Core
 * @author KeiMOon
 * @since 1.5
 */

 class Owl_Master_Template {
     /**
      * Layout file name. The default layout file can be set in the bootstrapping
      * file by using set_layout_file method of the Core class. If the template
      * class use another layout file, this member must be overrided in that
      * class. If layout file is not needed, for example the template class
      * only provides XML content such as RSS or Atom Feeds, this member must be
      * set to false
      *
      * @access protected
      * @param mixed $layout Layout file name
      */
     protected $layout = "default";

     /**
      * PHP5 Constructor. Do not override thi method, use the "init" method for
      * initialiaing instead.
      */
     function __construct() {
         $this->Owl_Master_Template();
     }

     /**
      * PHP4 Constructor. Do not override thi method, use the "init" method for
      * initialiaing instead.
      */
     function Owl_Master_Template() {
         $this->init();
         register_shutdown_function(array($this,"__destruct"));
     }

     /**
      * PHP5 Destructor
      *
      * @return false
      */
     function __destruct() {
        return false;
     }

     /**
      * Initialization method. This method should be overrided in the child class.
      * This method covers all actions that must be done before content outputting,
      * for example enqueuing styles and scripts, assigning content...
      */
     protected function init() {
         return false;
     }

     /**
      * Render method. This method will be call in the Core class for rendering
      * the content. Based on the member $layout, the content will be display with
      * or without a layout file. 
      *
      * @param string $layout
      */
     function render($layout) {
         if ($this->layout === false) {
             $this->render_content();
         }
         elseif ($this->layout == "default") {
            $this->load_file($layout);
         }
         else {
             $this->load_file($this->layout);
         }
     }

     /**
      * Method for rendering the main dynamic content. If the layout file is not set,
      * this method must be overrided in the child class to render the content.
      */
     protected function render_content($callback= false) {
         if (method_exists($this, $callback))
            return call_user_func(array($this, $callback));
     }

     /**
      * Safe loading file method
      *
      * @param string $file File name
      */
     function load_file($file) {
         if (file_exists(Owl_Theme_Control::get_current_theme_path() . $file)) {
             include_once(Owl_Theme_Control::get_current_theme_path() . $file);
         }
         elseif (file_exists(TEMPLATE_PATH . $file)) {
             include_once(TEMPLATE_PATH . $file);
         }
         else {

             Owl_Functions::hngk_die("The file $file is not found on the ".TEMPLATE_PATH." folder or "
                 . Owl_Theme_Control::get_current_theme_path() . " folder. Please check your installation");
         }
     }
 }

